package com.lanou.sfu.songforu.music_library_fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.RankAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.entity.RankEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class RankFragment extends BaseFragment {
    private ListView mListView;
    private Context context;
    private View v;
    private VolleySingleton volleySingleton;
    private StringRequest request;
    private List<RankEntity.Content> rankEntityList;
    private RankAdapter adapter;
    private ProgressDialog dialog;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.viewpager_rank, null);
        mListView = (ListView) v.findViewById(R.id.rank_listView);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        volleySingleton = VolleySingleton.getInstance(context);
        String url = Url.URL_RANK;
        request = new StringRequest(url, new Response.Listener<String>() {


            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RankEntity rankEntity = gson.fromJson(response,RankEntity.class);
                rankEntityList = rankEntity.getContent();
                if (rankEntityList != null) {
                    adapter = new RankAdapter(context, rankEntityList);
                }

                mListView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString(getString(R.string.name), rankEntityList.get(position).getName());
                bundle.putInt(getString(R.string.type), rankEntityList.get(position).getType());
                RankClassFragment rankClassFragment = new RankClassFragment();
                rankClassFragment.setArguments(bundle);
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fl_main,rankClassFragment).addToBackStack(null).commit();
            }
        });
    }
}
